package com.magic.java8.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
 * @author laoma
 * @create 2021-05-30 11:13
 */
public class LambdaTest2 {

    @Test
    public void test1() {
        ICalculator ic = i -> i * i;
        int square = ic.square(5);
        System.out.println("square = " + square);
    }

    @Test
    public void test2() {
        ICalculator ic = new ICalculator() {
            @Override
            public int square(int i) {
                return i + i;
            }
        };
        int square = ic.square(5);
        System.out.println("square = " + square);
    }

    @Test
    public void test3() {
        User user = new User();
        user.setUsername("zhangsan");
        String say = user.say((username) -> "hello," + username);
        System.out.println("say = " + say);
    }

    @Test
    public void test4() {
        User2 user2 = new User2();
        user2.setUsername("lisi");
        String say = user2.say((username) -> "hi," + username);
        System.out.println("say = " + say);
    }

    @Test
    public void test5() {
        User2 user2 = new User2();
        user2.setUsername("javaboy");
        Function<String, String> func = (username) -> "hello " + username;
        String say = user2.say(func.andThen(s -> "你好 " + s));
        System.out.println("say = " + say);
    }

    @Test
    public void test6() {
        List<String> names = Arrays.asList("张三", "李四", "张三丰");
        List<String> list = names.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        list.forEach(item->{
            System.out.println("item = " + item);
        });
    }

    @Test
    public void test7(){
        List<String> names = Arrays.asList("张三", "李四", "张三丰");
        names.stream().forEach(s-> System.out.println("s = " + s));
    }
}
